/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.chl.da.controllerBeans;

import edu.chl.da.ejb.core.Customer;
import edu.chl.da.ejb.core.Flight;
import edu.chl.da.ejb.core.Seat;
import edu.chl.da.ejb.ctrl.IManageAirplane;
import edu.chl.da.ejb.ctrl.IManageCustomer;
import edu.chl.da.ejb.ctrl.IManageFlight;
import edu.chl.da.ejb.ctrl.IManageReservation;
import javax.ejb.EJB;
import javax.el.ELContext;
import javax.el.ValueExpression;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

/**
 * Controller bean for add reservation evetns.
 * @author Erik
 */
@ManagedBean(name = "AddReservationBean")
@SessionScoped
public class AddReservationBean {

    @EJB
    IManageReservation imr;

    @EJB
    IManageFlight imf;

    @EJB
    IManageAirplane ima;

    @EJB
    IManageCustomer imc;

    /** Creates a new instance of AddReservationBean */
    public AddReservationBean() {

    }

    public void addReservation(ActionEvent e){

        FacesContext context = FacesContext.getCurrentInstance();
        ELContext elContext = context.getELContext();
        ValueExpression ex =
           context.getApplication().getExpressionFactory().createValueExpression(elContext,"#{ReservationBean}", ReservationBean.class);
        ValueExpression ex2 =
           context.getApplication().getExpressionFactory().createValueExpression(elContext,"#{LoginBean}", LoginBean.class);

        ReservationBean reservation = (ReservationBean) ex.getValue(elContext);
        LoginBean loginBean = (LoginBean) ex2.getValue(elContext);

        String accName = loginBean.getAccountName();
        Flight flight = reservation.getFlight();
        Customer customer = imc.getCustomer(accName);
        Seat seat = imr.getFirstSeat(flight.getAirplane1());
        imr.createReservation(flight, customer, seat);
    }

    public String navigate(){
        return "/views/receipt.jsf?faces-redirect=true";
    }
}